package com.runa.monitor.platinterface.netservice.equipment;

import com.runa.monitor.comm.dto.Archive;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import com.runa.rccprotocol.dto.qiyang.QyRequestData;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 2024/7/22 19:08
 * zhangYB
 */
@Slf4j
public class QyDriver extends NetEqu<String> {


    public QyDriver(String serial, Integer timeout) {
        super(serial, timeout);
    }

    @Override
    public void readNodeEqu(List<Node> nodeList, TaskFunction function, EquData.EquDataType type) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        switch (type) {
            case METER:
                readMeter(nodeList, function);
                break;
            case VALVE:
            case PUBLIC_UNIT:
                readValve(nodeList, function);
                break;
        }
    }

    private void readMeter(List<Node> nodeList, TaskFunction function) {
        for (Node node : nodeList) {
            QyRequestData qyRequestData = new QyRequestData(serial());
            qyRequestData.readMeter(node.getSerial());
            log.debug("{} 抄表: {}", serial(), ByteUtil.byteToHexString(qyRequestData.getData()));
            qyRequestData.setRemain(Collections.singletonList(node));
            sendRequest(qyRequestData, function);
        }
    }

    private void readValve(List<Node> nodeList, TaskFunction function) {
        for (Node node : nodeList) {
            QyRequestData qyRequestData = new QyRequestData(serial());
            qyRequestData.readValve(node.getSerial());
            log.debug("{} 抄阀: {}", serial(), ByteUtil.byteToHexString(qyRequestData.getData()));
            qyRequestData.setRemain(Collections.singletonList(node));
            sendRequest(qyRequestData, function);
        }
    }

    @Override
    public void valveOnOffControl(List<Node> nodeList, boolean ifLock, Integer opening, TaskFunction function) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        for (Node node : nodeList) {
            QyRequestData qyRequestData = new QyRequestData(serial());
            qyRequestData.valveControlOpen(node.getSerial(), opening != null && (opening != 0));
            log.debug("{} 控阀: {}", serial(), ByteUtil.byteToHexString(qyRequestData.getData()));
            qyRequestData.setRemain(node);
            sendRequest(qyRequestData, function);
        }
    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> nodeList, Integer model, Map param, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitModel(List<Node> nodeList, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valve, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valve, Node meter, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }
}
